from django.contrib.auth.backends import ModelBackend
import re
from .models import User
from django.db.models import Q


class UsernameMobileAuthBackend(ModelBackend):
    def authenticate(self,request,username=None,password=None,**kwargs):
        try:
            user = User.objects.get(Q(username=username)|Q(mobile=username))
        except:
            return None
        if user.check_password(password):
            return user



